package com.example.xueshen.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xueshen.common.entity.*;
import com.example.xueshen.common.service.CourseService;
import com.example.xueshen.common.service.TeacherService;
import com.example.xueshen.common.util.MyPageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private TeacherService teacherService;

    @GetMapping("/toAdd1")
    public String toAdd(Map<String, Object> map) {
        List<Teacher> teachers = teacherService.getAll();
        map.put("teachers", teachers);
        return "student/course :: addOrUpdate";
    }

    @GetMapping("/course")
    public String select(PageData pageData, Map<String, Object> map, String courseName) {
        Page<Course> page = new Page<>(pageData.getCurrent(), pageData.getSize());
        if (courseName !=null)
        {

            Page<Course> coursePage = courseService.selectByLike(courseName, page);
            for (Course course : coursePage.getRecords()) {
                Teacher teacher = teacherService.getById(course.teacher.getTeacherId());
                course.setTeacher(teacher);
            }
            if (page.getTotal() != 0) {
                int[] pageNums = MyPageUtil.getNavigatePageNums((int) coursePage.getCurrent(), (int) coursePage.getPages(), 3);
                map.put("pageNums", pageNums);
            }
            map.put("courseName", courseName);
            map.put("page", coursePage);

        }else {

            Page<Course> coursePage = courseService.selectBy(page);


            for (Course course : coursePage.getRecords()) {
                Teacher teacher = teacherService.getById(course.getTeaTeacherId());
                course.setTeacher(teacher);
            }

            for (Course course : coursePage.getRecords()) {
                Teacher teacher = teacherService.getById(course.getTeaTeacherId());

                course.setTeacher(teacher);

                if (page.getTotal() != 0) {
                    int[] pageNums = MyPageUtil.getNavigatePageNums((int) coursePage.getCurrent(), (int) coursePage.getPages(), 3);
                    map.put("pageNums", pageNums);
                }

                map.put("page", coursePage);
            }
        }
        return "student/course";
    }

    @PostMapping("/courses1")
    @ResponseBody
    public String add(@Valid Course course) {
        if (course.getCourseId() != null) {
            Boolean flag = courseService.update(course);
            return flag ? "修改成功" : "修改失败";
        }
        Boolean flag = courseService.toAdd(course);
        return flag ? "添加成功" : "添加失败";
    }


    @GetMapping("/course/{courseId}")
    public String selectById(@PathVariable("courseId") Integer cid, Map<String, Object> map) {
        Course courses = courseService.getById(cid);
        List<Teacher> teachers = teacherService.getAll();
        map.put("teachers", teachers);
        map.put("course", courses);
        return "student/course :: addOrUpdate";
    }


    @DeleteMapping("/delete1/{courseId}")
    @ResponseBody
    public Boolean delete(@PathVariable("courseId") Integer id) {
        courseService.deleteById(id);
        return  true;
    }

/*
 @GetMapping("/course/{courseName}")
    public String selectLike(@RequestParam(required = false ) @PathVariable("courseName")  String courseName, Map<String, Object> map, PageData pageData) {
     Page<Course> page = new Page<>(pageData.getCurrent(), pageData.getSize());
     //模糊查询
     Page<Course> coursePage = courseService.selectByLike(courseName, page);

     for (Course course : coursePage.getRecords()) {
         Teacher teacher = teacherService.getById(course.getTeaTeacherId());
         course.setTeacher(teacher);

     }
     if (page.getTotal() != 0) {
         int[] pageNums = MyPageUtil.getNavigatePageNums((int) coursePage.getCurrent(), (int) coursePage.getPages(), 3);
         map.put("pageNums", pageNums);
     }

     map.put("page", coursePage);
     return "student/course";
 }
*/

}
